// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: EncryptData.java

package com.market.web.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class EncryptData
{
    private static final Logger logger = LoggerFactory.getLogger(EncryptData.class.getName());
    
    public EncryptData()
    {
    }

    public static String encrypt(String password)
    {
        byte encrypted_pwd[] = (byte[])null;
        try
        {
            cipher.init(1, theKey, IvParameters);
            byte plainttext[] = password.getBytes();
            encrypted_pwd = cipher.doFinal(plainttext);
        }
        catch (Exception ex)
        {
            logger.error("",ex);
        }
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < encrypted_pwd.length; i++)
            str.append(",").append(encrypted_pwd[i]);

        return str.toString();
    }

    public static String decrypt(String password)
    {
        String decrypted_password = null;
        String str[] = password.split(",");
        try
        {
            cipher.init(2, theKey, IvParameters);
            byte decryptedPassword[] = new byte[str.length - 1];
            for (int i = 1; i < str.length; i++)
                decryptedPassword[i - 1] = (new Byte(str[i])).byteValue();

            byte decrypted_pwd[] = cipher.doFinal(decryptedPassword);
            decrypted_password = new String(decrypted_pwd);
        }
        catch (Exception ex)
        {
            logger.error("",ex);
        }
        return decrypted_password;
    }

    //private static final String DESEDE_KEY = "CHINESE Chong Qing market 023";

    private static byte encryptKey[];

    private static DESedeKeySpec spec;

    private static SecretKeyFactory keyFactory;

    private static SecretKey theKey;

    private static Cipher cipher;

    private static IvParameterSpec IvParameters;

    static
    {
        try
        {
            /*Cipher c = null;
            try
            {
                c = Cipher.getInstance("DESede");
                
            }
            catch (Exception e)
            {
                System.err.println("Installling SunJCE provider.");
                java.security.Provider sunjce = new SunJCE();
                Security.addProvider(sunjce);
            }*/
            encryptKey = "CHINESE Chong Qing market 023".getBytes();
            spec = new DESedeKeySpec(encryptKey);
            keyFactory = SecretKeyFactory.getInstance("DESede");
            theKey = keyFactory.generateSecret(spec);
            cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            IvParameters = new IvParameterSpec(new byte[]{25, 75, 56, 70, 20, 0, 14, 49});
        }
        catch (Exception exc)
        {
            logger.error("",exc);
        }
    }
}
